//
//  ViewController.m
//  数据连接
//
//  Created by KM on 16/10/19.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "ViewController.h"
#import "LoginData.h"
#import "ListViewController.h"
#import "RefrechViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *passWord;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)onClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.userName.text,self.passWord.text]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *text = [[NSString alloc]initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        LoginData *d = [[LoginData alloc]initWithDictionary:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新界面代码
            
            if ( d.errcode== 0)
            {
                //ListViewController *lists = [self.storyboard instantiateViewControllerWithIdentifier:@"list"];
                //[self.navigationController pushViewController:lists animated:YES];
                RefrechViewController *res = [self.storyboard instantiateViewControllerWithIdentifier:@"re"];
                [self.navigationController pushViewController:res animated:YES];
            }
            else
            {
                NSLog(@"%@",d.errinfo);
            }
        });
    }];
    [task resume];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
